import { defineConfig, loadEnv, splitVendorChunkPlugin, splitVendorChunk } from 'vite';
import vue from '@vitejs/plugin-vue2';
import vueJsx from "@vitejs/plugin-vue2-jsx";
import { join } from 'path'
import { writeFileSync } from 'fs'

import path from 'path'

// const html = require('@rollup/plugin-html');
import { chunkSplitPlugin } from 'vite-plugin-chunk-split';
import _ from 'lodash';



// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const PROCESS_CONSTAINT = loadEnv(mode, process.cwd())
  const VITE_MODE=PROCESS_CONSTAINT.VITE_MODE
  const VITE_PUBLIC_PATH=PROCESS_CONSTAINT.VITE_PUBLIC_PATH

  return {
    base: VITE_PUBLIC_PATH,
    resolve: {
      alias: {
        '@': '/src',
      },
    },
    plugins: [
      vue(),
      vueJsx(),
    // chunkSplitPlugin({
      //   strategy: 'single-vendor',
      //   // 指定拆包策略
      //   customSplitting: {
      //     "elementui-vendor": ['element-ui'],
      //     'vue-vendor': ['vue'],
      //     'common-vendor': ['svgIcon', 'utils', 'mydialogplus']
      //   }
      // }),

      // lifecycle === 'report'
      //   ? visualizer({ open: true, brotliSize: true, filename: 'report.html' })
      //   : null,
    ],
    server: {
      //force: true,
//  host:'192.168.11.250',
      port: 88,
      proxy: {
        '/api': {
          target: 'http://localhost:17001/', // 代理的目标地址
          // target: 'http://api.szqerp.com/', // 代理的目标地址
          changeOrigin: true, // 开发模式，默认的origin是真实的 origin:lo
          rewrite: (path) => path.replace(/^\/api/, '/api')
        },
      }
    },
    build: {
      target: 'esnext',
      minify: false,
      cssCodeSplit: true,
      rollupOptions: {
        // external: ['element-ui'],
        input: {
          index: "./index.html",
        },
       
        output: {
          manualChunks(id) {
            console.log('manualChunks id',id);
            writeContent(id)
            if (id.includes('lodash')) {
              return 'chunk-lodash';
            }
            if (id.includes('/vue@')) {
              return 'vue';
            }
            if (id.includes('element-ui@')) {
              return 'chunk-element-ui';
            }
            if (id.includes('core-js@')) {
              return 'chunk-core-js';
            }
            if (id.includes('utils/dist/utils.js')) {
              return 'chunk-utils';
            }
            if (id.includes('@base/svg-icon')) {
              return 'chunk-svgIcon';
            }
            if (id.includes('departSelectOne')) {
              return 'chunk-departSelectOne';
            }
            
          
          }
        }

      }

    },
  }

})


const os = require("os");
const fs =require('fs')

function writeContent(content){
  fs.appendFile("./chunknames.txt",content+'\n'+os.EOL,()=>{});

}
